package test.testcase.dao;

import test.user.user;
import test.utils.GetValue;
import test.utils.JsonResult;
import test.utils.Mysql;

public class UserDao {
	
	public static JsonResult getUser(String userid) {
		return Mysql.select("select * from user where id="+userid);
	}
	
	public static JsonResult addUser(String username, String pwd) {
		if (Mysql.select("select count(*) as count from user where username='"+username+"'").read("data[0].count").toInt() >0) {
			return null;
		}
		return Mysql.excute("insert into user (username,pwd) values('"+username+"','"+pwd+"')");
	}
	
	public static JsonResult getUserByUserName(String username) {
		return Mysql.select("select * from user where username='"+username+"'");
	}
	
	public static JsonResult saveToken(user user, int num) {
		Mysql.excute("delete from token where token='"+user.getUsername()+"'");
		return Mysql.excute("insert into token (token,date,randuid) values('"+user.getUsername()+"',now(),"+num+")");
	}
	
	public static JsonResult getToken(String username, String num) {
		return Mysql.select("select * from `token` where `token`='"+username+"' and `date`='"+GetValue.getDateStr(0)+"' and randuid="+num);
	}
	
	public static JsonResult deleteToken(String username) {
		return Mysql.excute("delete from token where token='"+username+"'");
	}
	
	public static JsonResult list() {
		return Mysql.select("select * from user");
	}
	
	public static JsonResult projectuserlist(String projectid) {
		return Mysql.select("select a.* from user a left join user_project b on a.id=b.userid where b.projectid="+projectid);
	}
	
	public static JsonResult repwd(String id) {
		return Mysql.excute("update user set pwd='123456' where userid="+id);
	}
	
	public static JsonResult repwd(String username, String pwd) {
		return Mysql.excute("update user set pwd='"+pwd+"' where username='"+username+"'");
	}

}
